package com.yongcheon.sms_manager.notification;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import com.yongcheon.sms_manager.R;
import com.yongcheon.sms_manager.activities.CancelNotifationActivity;
import com.yongcheon.sms_manager.receiver.phone.PhoneReceiver;
import com.yongcheon.sms_manager.receiver.sms.SmsReceiver;
import com.yongcheon.sms_manager.utils.AndroidSystemUtil;
import com.yongcheon.sms_manager.vo.SpamNumVO;

/**
 * Created by yongcheon on 2014-12-29.
 */
public class SpamPhoneNotification {

    public void setNotification(Context context, SpamNumVO spamVO) {
        NotificationManager mNotiManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
        Intent cancelIntent = new Intent(context.getApplicationContext(), CancelNotifationActivity.class);
        PendingIntent pCancelIntent = PendingIntent.getActivity(context.getApplicationContext(), PhoneReceiver.NOTI_ID, cancelIntent, PendingIntent.FLAG_CANCEL_CURRENT );

        if(AndroidSystemUtil.ANDROID_VERSION < 16) {
            setNoti15(context, mNotiManager, spamVO, pCancelIntent);
        }else{
            setNoti16(context, mNotiManager, spamVO, pCancelIntent);
        }
    }

    public void setNoti15(Context context, NotificationManager mNotiManager, SpamNumVO spamVO, PendingIntent pCancelIntent){
        String title = context.getResources().getString(R.string.spam_phone_arrive);
        String contextText = " ( " + spamVO.getCategory_2() + " ) "+spamVO.getAddress() ;
        Notification notification = new Notification(R.drawable.ic_launcher_1, title, System.currentTimeMillis());
        notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
        notification.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE ;
        notification.setLatestEventInfo(context, title, contextText, pCancelIntent);
        mNotiManager.notify(PhoneReceiver.NOTI_ID, notification);
    }

    public void setNoti16(Context context, NotificationManager mNotiManager, SpamNumVO spamVO, PendingIntent pCancelIntent){
        Notification.Builder notiBuilder = new Notification.Builder(context.getApplicationContext())
                .setSmallIcon(R.drawable.ic_launcher_1)
                .setContentTitle(context.getResources().getString(R.string.spam_phone_arrive))
//                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher_1))
                .setPriority(1)
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(true)
                .setContentIntent(pCancelIntent)
                .setTicker(context.getResources().getString(R.string.spam_phone_arrive));

        Notification.InboxStyle inboxStyle = new Notification.InboxStyle(notiBuilder);
        inboxStyle.addLine(context.getResources().getString(R.string.spam_number)+" : "+spamVO.getAddress());
        inboxStyle.addLine(context.getResources().getString(R.string.spam_type)+" : "+spamVO.getCategory_2());
        Notification noti = inboxStyle.build();
        noti.flags |= Notification.FLAG_ONGOING_EVENT;
        noti.flags |= Notification.FLAG_NO_CLEAR;

        mNotiManager.notify(SmsReceiver.NOTI_ID, noti);

    }
}
